package com.lsyw.intergrate.serviceImpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.StringUtil;
import com.lsyw.intergrate.entity.TransportWorkOrder;
import com.lsyw.intergrate.entity.TransportWorkOrderAlarm;
import com.lsyw.intergrate.mapper.RouteMapper;
import com.lsyw.intergrate.mapper.TransportWorkOrderAlarmMapper;
import com.lsyw.intergrate.service.TransportWorkOrderAlarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 运输工单告警信息 服务实现类
 * </p>
 *
 * @author 刘磊
 * @since 2023-12-14
 */
@Service
public class TransportWorkOrderAlarmServiceImpl extends ServiceImpl<TransportWorkOrderAlarmMapper, TransportWorkOrderAlarm> implements TransportWorkOrderAlarmService {
    @Autowired
    private RouteMapper routeMapper;

    /**
     *
     * @param latitudeAndLongitude 经纬度
     * @param transportWorkOrder 工单对象
     * @param type 类型（1：线路偏移，2：土壤到期未销毁）
     * @param gpsNo gps设备号
     */
    @Override
    public void saveAlarm(String latitudeAndLongitude,TransportWorkOrder transportWorkOrder,int type,String gpsNo) {
        TransportWorkOrderAlarm transportWorkOrderAlarm = new TransportWorkOrderAlarm();
        transportWorkOrderAlarm.setTransportWorkOrderId(transportWorkOrder.getId());
        transportWorkOrderAlarm.setAlarmDate(new Date());
        transportWorkOrderAlarm.setLatitudeAndLongitude(latitudeAndLongitude);
        if (type==1){
            if(ObjectUtil.isNotNull(transportWorkOrder.getGpsVehicleId())&&gpsNo.equals(transportWorkOrder.getGpsVehicleId())){
                transportWorkOrderAlarm.setType(1);
            }else{
                transportWorkOrderAlarm.setType(3);
            }

        }else{
            transportWorkOrderAlarm.setType(type);
        }

        transportWorkOrderAlarm.setStatus(1);
        transportWorkOrderAlarm.setGmtCreate(new Date());
        transportWorkOrderAlarm.setDelFlag(0);
        this.save(transportWorkOrderAlarm);
    }

    @Override
    public IPage<TransportWorkOrderAlarm> pageList(Page<TransportWorkOrderAlarm> page, Map<String, Object> condition) {
        return baseMapper.pageList(page, condition);
    }

    @Override
    public TransportWorkOrderAlarm getInfoById(String id) {
        TransportWorkOrderAlarm transportWorkOrderAlarm =baseMapper.getInfoById(id);
        transportWorkOrderAlarm.setRoute(routeMapper.selectById(transportWorkOrderAlarm.getRouteId()));
        return transportWorkOrderAlarm;
    }
}
